package com.sky.wsp.mybatis.plus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sky.wsp.mybatis.plus.utils.SecurityContextHolder;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Calendar;
import java.util.Date;

/**
 * Mybatis元数据填充处理类，仅能处理MP的函数，不能处理mapper.xml中自定义的insert、update
 *
 * @author wangshaopeng@talkweb.com.cn
 * @Date 2023-01-11
 */
public class DBMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Date currentDate = Calendar.getInstance().getTime();

        this.strictInsertFill(metaObject, "createTime", Date.class, currentDate);
        this.strictInsertFill(metaObject, "createBy", Long.class, SecurityContextHolder.getUserId());
        this.strictInsertFill(metaObject, "updateTime", Date.class, currentDate);
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date currentDate = Calendar.getInstance().getTime();
        this.strictUpdateFill(metaObject, "updateTime", Date.class, currentDate);
    }
}
